using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MvcApplication.Models;

namespace MvcApplication.Controllers
{
    public class MusicController : Controller
    {
        private MusicsDBEntities _db = new MusicsDBEntities();

        //
        // GET: /Music/

        public ActionResult MusicList()
        {
            return View(_db.MusicSet.ToList());
        }

        //
        // GET: /Music/Details/5

        public ActionResult Details(int id)
        {
            var c = _db.MusicSet.Where(p => p.Id == id).First();
            return View(c);
        }

        //
        // GET: /Music/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Music/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "Id")]Music newMusic)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
            try
            {
                _db.AddToMusicSet(newMusic);
                _db.SaveChanges();

                return RedirectToAction("MusicList");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Music/Edit/5

        public ActionResult Edit(int id)
        {
            var c = _db.MusicSet.Where(p => p.Id == id).First();
            return View(c);
        }

        //
        // POST: /Music/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Music newMusic)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
            try
            {
                var oldEntity = _db.MusicSet.Where(p => p.Id == newMusic.Id).First();
                _db.ApplyPropertyChanges(oldEntity.EntityKey.EntitySetName, newMusic);
                _db.SaveChanges();
                return RedirectToAction("MusicList");
            }
            catch
            {
                return View();
            }
        }
    }
}
